package es.ats.codington.festival.services;

import java.sql.Connection;
import java.util.ArrayList;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.interfaces.services.IVisitorService;
import es.ats.codington.festival.daos.VisitorDAOImpl;
import es.ats.codington.festival.services.ServiceManager;

/**
 * The Class VisitorServiceImpl.
 */
public class VisitorServiceImpl implements IVisitorService{
	
	/**
	 * Show visitor.
	 *
	 * @param visitor the visitor
	 * @return true, if successful
	 * @see es.ats.codington.festival.interfaces.services.IVisitorService#showVisitor(es.ats.codington.festival.domain.Visitor)
	 */
	public boolean showVisitor(Visitor visitor){
		/*
		System.out.println("New Visitor Data: ");
		System.out.println("ID: "+visitor.getVisitorId());
		System.out.println("Name: "+visitor.getName()+" "+visitor.getLastName());
		System.out.println("Age: "+visitor.getAge());
		System.out.println("Address: "+visitor.getAddress());
		System.out.println("Username: "+visitor.getUsername());
		System.out.println("Password: "+visitor.getPassword());
		System.out.println("Phone Number: "+visitor.getPhoneNumber());
		System.out.println("Admin: "+visitor.isAdmin());*/
		return true;
	}

	/**
	 * Register visitor.
	 *
	 * @param visitor the visitor
	 * @return true, if successful
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.services.IVisitorService#registerVisitor(es.ats.codington.festival.domain.Visitor)
	 */
	public boolean registerVisitor (Visitor visitor) throws Exception{
		Connection con = null;
		//int row = 0;
		
		ServiceManager serviceManager = new ServiceManager();
		VisitorDAOImpl visitorDAO = new VisitorDAOImpl();
		try{
			con = serviceManager.OpenConnectionToDB();
			visitorDAO.registerVisitorDAO(con, visitor);
			serviceManager.CloseConnectionToDB(con);
			//Printing how many rows got added
			//System.out.println(row + " User added!");
			
			return true;
		}catch(Exception e){
			System.out.println(e.getMessage());
		}		
		return false;
	}
	
	/**
	 * Delete visitor.
	 *
	 * @param idVisitor the id visitor
	 * @return true, if successful
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.services.IVisitorService#deleteVisitor(int)
	 */
	public boolean deleteVisitor (int idVisitor) throws Exception{
		Connection con = null;
		//int row = 0;
		
		ServiceManager serviceManager = new ServiceManager();
		VisitorDAOImpl visitorDAO = new VisitorDAOImpl();
		try{
			con = serviceManager.OpenConnectionToDB();
			visitorDAO.deleteVisitorDAO(con, idVisitor);
			serviceManager.CloseConnectionToDB(con);
			
			//Printing how many rows got added
			//System.out.println(row + " User deleted!");
			return true;
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return false;
	}

	/**
	 * Update visitor.
	 *
	 * @param visitor the visitor
	 * @return true, if successful
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.services.IVisitorService#updateVisitor(es.ats.codington.festival.domain.Visitor)
	 */
	public boolean updateVisitor(Visitor visitor)throws Exception{
		Connection con = null;
		//int row = 0;
		
		ServiceManager serviceManager = new ServiceManager();
		VisitorDAOImpl visitorDAO = new VisitorDAOImpl();
		try{
			con = serviceManager.OpenConnectionToDB();
			visitorDAO.updateVisitorDAO(con, visitor);
			serviceManager.CloseConnectionToDB(con);
			
			//Printing how many rows got added
			//System.out.println(row + " User updated!");
			
			return true;
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return false;
	}
	
	/**
	 * Search visitor by id.
	 *
	 * @param idVisitor the id visitor
	 * @return the visitor
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.services.IVisitorService#searchVisitorById(int)
	 */
	public Visitor searchVisitorById (int idVisitor) throws Exception{
		Visitor visitor = new Visitor();
		Connection con = null;
		//int row = 0;
		
		ServiceManager serviceManager = new ServiceManager();
		VisitorDAOImpl visitorDAO = new VisitorDAOImpl();
		try{
			con = serviceManager.OpenConnectionToDB();
			visitor = visitorDAO.searchVisitorDAOById(con, idVisitor);
			serviceManager.CloseConnectionToDB(con);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		//if (visitor.getUsername()!= null){
			//row++;
		//}
		//Printing how many rows got added
		//System.out.println(row + " User found!");
		
		return visitor;
	}
	
	/**
	 * Search visitor by username.
	 *
	 * @param username the username
	 * @return the visitor
	 * @throws Exception the exception
	 */
	public Visitor searchVisitorByUsername (String username) throws Exception{
		Visitor visitor = new Visitor();
		Connection con = null;
		//int row = 0;
		
		ServiceManager serviceManager = new ServiceManager();
		VisitorDAOImpl visitorDAO = new VisitorDAOImpl();
		try{
			con = serviceManager.OpenConnectionToDB();
			visitor = visitorDAO.searchVisitorDAOByUsername(con, username);
			serviceManager.CloseConnectionToDB(con);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		if (visitor.getUsername()!= null){
			//row++;
		}
		//Printing how many rows got added
		//System.out.println(row + " User found!");
		
		return visitor;
	}
	
	/**
	 * Login visitor.
	 *
	 * @param username the username
	 * @param password the password
	 * @return the visitor
	 * @see es.ats.codington.festival.interfaces.services.IVisitorService#loginVisitor(java.lang.String, java.lang.String)
	 */
	public Visitor loginVisitor(String username, String password){	
		ServiceManager serviceManager = new ServiceManager();
		Connection con = null;
		VisitorDAOImpl visitorDAO = new VisitorDAOImpl();
		Visitor visitor = new Visitor();
		
		try{
			con = serviceManager.OpenConnectionToDB();
			visitor = visitorDAO.searchVisitorDAOByUsername(con, username);
			serviceManager.CloseConnectionToDB(con);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		if ((visitor.getVisitorId()!=0)&&(visitor.getPassword().equals(password))){		
		//	System.out.println("User logged!");
		}else{
		//	System.out.println("Login failed!");
		}
		return visitor;
	}
	
	/**
	 * Search all visitors.
	 *
	 * @return the array list
	 * @see es.ats.codington.festival.interfaces.services.IVisitorService#searchAllVisitors()
	 */
	public ArrayList<Visitor> searchAllVisitors() {
		Connection con = null;
		ServiceManager serviceManager = new ServiceManager();
		VisitorDAOImpl visitorDAO = new VisitorDAOImpl();

		ArrayList<Visitor> visitors = new ArrayList<Visitor>();
		try{
			con = serviceManager.OpenConnectionToDB();			
			visitors = visitorDAO.searchAllVisitorDAO(con);
			serviceManager.CloseConnectionToDB(con);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return visitors;
	}
}
